//
//  KATProgressBar.m
//  KATFramework
//
//  Created by Kantice on 16/6/17.
//  Copyright © 2016年 KatApp. All rights reserved.
//

#import "KATProgressBar.h"

@implementation KATProgressBar


#pragma -mark 类方法

//获取实例
+ (instancetype)progressBarWithFrame:(CGRect)frame
{
    KATProgressBar *bar=[[[self alloc] init] autorelease];
        
    bar.animationDuration=0.2;
    bar.bgColorAlpha=0.3;
    bar.frontIndent=0;
    bar.hasRoundCorner=YES;
    bar.color=COLOR_GRAY;
    
    bar.frame=frame;
    
    //至零
//    [bar progressWithLeftValue:0 rightValue:0 animated:NO];
    
    
    return bar;
}



#pragma -mark 对象方法


- (double)width
{
    return self.frontBar.frame.size.width;
}

- (double)startPointX
{
    return self.frame.origin.x+self.frontBar.frame.origin.x;
}


- (void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
    
    [self updateBar];
}


//刷新
- (void)updateBar
{
    //先删除原先内容
    if(_bgBar)
    {
        [_bgBar removeFromSuperlayer];
    }
    
    if(_frontBar)
    {
        [_frontBar removeFromSuperlayer];
    }
    
    //背景条
    self.bgBar=[KATShapeUtil roundedRectShapeWithWidth:self.bounds.size.width andHeight:self.bounds.size.height andCornerRadius:_hasRoundCorner?self.bounds.size.height*0.5:0];
    _bgBar.frame=self.bounds;
    _bgBar.fillColor=COLOR(_color-_color%COLOR_HSBA_B+((int)(_bgColorAlpha*99))%100).CGColor;
    _bgBar.strokeColor=[UIColor clearColor].CGColor;
    [self.layer addSublayer:_bgBar];
    
    //前景条
    self.frontBar=[KATShapeUtil lineShapeWithStartPoint:CGPointMake(0, 0) andEndPoint:CGPointMake(self.bounds.size.width-self.bounds.size.height, 0)];
    _frontBar.frame=CGRectMake(self.bounds.size.height/2, self.bounds.size.height/2.0, _frontBar.bounds.size.width, _frontBar.bounds.size.height);
    _frontBar.animationTimingMode=kCAMediaTimingFunctionLinear;
    _frontBar.strokeColor=COLOR(_color).CGColor;
    _frontBar.fillColor=[UIColor clearColor].CGColor;
    _frontBar.lineCap=(_hasRoundCorner?kCALineCapRound:kCALineCapSquare);
    _frontBar.lineWidth=self.bounds.size.height-_frontIndent*2;
    [self.layer addSublayer:_frontBar];
    
    _frontBar.strokeStart=_leftValue;
    _frontBar.strokeEnd=_rightValue;
    
}


//设置左右值
- (void)progressWithLeftValue:(double)leftValue rightValue:(double)rightValue animated:(BOOL)animated
{
    if(leftValue<0)
    {
        leftValue=0;
    }
    
    if(rightValue>1.0)
    {
        rightValue=1.0;
    }
    
    if(leftValue>rightValue)
    {
        leftValue=rightValue;
    }
    
    
    double dif=(fabs(rightValue-_rightValue)>fabs(leftValue-_leftValue)?fabs(rightValue-_rightValue):fabs(leftValue-_leftValue));
    
    _leftValue=leftValue;
    _rightValue=rightValue;
    
    
    if(animated)
    {
//        [UIView animateWithDuration:(fabs(leftValue-_leftValue)>fabs(rightValue-_rightValue)?fabs(leftValue-_leftValue):fabs(rightValue-_rightValue))*_frontBar.frame.size.width*_animationSpeed animations:^
//        {
//            _leftValue=leftValue;
//            _rightValue=rightValue;
//            
//            _frontContanier.frame=CGRectMake(_frontIndent+_leftValue*_frontBar.frame.size.width, _frontContanier.frame.origin.y, _frontBar.frame.size.width*(_rightValue-_leftValue), _frontContanier.frame.size.height);
//            
//            _frontBar.frame=CGRectMake(-_frontBar.frame.size.width+_rightValue*_frontBar.frame.size.width, 0, _frontBar.frame.size.width, _frontBar.frame.size.height);
//        }];
        [self clearAnimations];
        
        [_frontBar strokeRangeWithStart:_leftValue end:_rightValue duration:dif*_animationDuration+0.12];
    }
    else
    {
//        _leftValue=leftValue;
//        _rightValue=rightValue;
//        
//        _frontContanier.frame=CGRectMake(_frontIndent+_leftValue*_frontBar.frame.size.width, _frontContanier.frame.origin.y, _frontBar.frame.size.width*(_rightValue-_leftValue), _frontContanier.frame.size.height);
//        
//        _frontBar.frame=CGRectMake(-_frontBar.frame.size.width+_rightValue*_frontBar.frame.size.width, 0, _frontBar.frame.size.width, _frontBar.frame.size.height);
        
        [self clearAnimations];
        
        _frontBar.strokeStart=_leftValue;
        _frontBar.strokeEnd=_rightValue;
        
    }
    
    
}


//设置右数值，左数值为0
- (void)progressWithValue:(double)value animated:(BOOL)animated
{
    [self progressWithLeftValue:0 rightValue:value animated:animated];
}



///释放内存
- (void)dealloc
{
    [_bgBar release];
//    [_frontContanier release];
    [_frontBar release];
    
    [super dealloc];
}


@end



